package com.dcivision.framework.taglib.channel;

import java.sql.Connection;
import java.util.Iterator;
import java.util.List;

import javax.servlet.jsp.PageContext;

import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.dcivision.calendar.bean.CalendarRecord;
import com.dcivision.framework.DataSourceFactory;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.MessageResourcesFactory;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.Utility;
import com.dcivision.framework.bean.SysFunction;
import com.dcivision.framework.dao.SysFunctionDAObject;
import com.dcivision.user.bean.UserDelegation;
import com.dcivision.user.dao.UserDelegationDAObject;
import com.dcivision.workflow.dao.WorkflowProgressDAObject;

public class FilterFactory {
  
  private static final Log log = LogFactory.getLog(FilterFactory.class);
  
  public static Filter getFilterByName(PageContext pageContext,String filterName){
    if(AjaxConstant.MEETINGFILTER.equalsIgnoreCase(filterName)){
      return  getMeetingFilter(pageContext,AjaxConstant.MEETINGFILTER);
    }
    if(AjaxConstant.TASKINBOXFILTER.equalsIgnoreCase(filterName)){
      return getTaskInboxFilter(pageContext,AjaxConstant.TASKINBOXFILTER);
    }
    if(AjaxConstant.MESSAGEINBOXFILTER.equalsIgnoreCase(filterName)){
      return getMessageInboxFilter(pageContext,AjaxConstant.MESSAGEINBOXFILTER);
    }
    return null;
  }
  
  private static Filter getMeetingFilter(PageContext pageContext,String filterName){
    //if(filterMap.get(filterName)==null){
      SessionContainer sessionContainer = (SessionContainer)pageContext.getSession().getAttribute(GlobalConstant.SESSION_CONTAINER_KEY);
    
    Filter filter = new SelectionFilter();
    filter.setName(filterName);
    FilterItem item = new SelectionFilterItem();
    item.setName(MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "common.options.select_menu.all"));
    item.setValue("-");
    filter.addItem(item);
    item = new SelectionFilterItem();
    item.setName(MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "calendar.columnheader.meeting_list"));
    item.setValue(CalendarRecord.EVENT_TYPE_CALENDAR_MEETING);
    filter.addItem(item);
    item = new SelectionFilterItem();
    item.setName(MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "calendar.columnheader.event_list"));
    item.setValue(CalendarRecord.EVENT_TYPE_CALENDAR_EVENT);
    filter.addItem(item);
    //filterMap.put(filterName, filter);
    //}
    //return (Filter)filterMap.get(filterName);
    return filter;
  }
  
  private static Filter getMessageInboxFilter(PageContext pageContext, String filterName) {
    SessionContainer sessionContainer = (SessionContainer) pageContext.getSession().getAttribute(GlobalConstant.SESSION_CONTAINER_KEY);
    Filter filter = new SelectionFilter();
    filter.setName(filterName);

    FilterItem item = new SelectionFilterItem();
    item.setName(MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "common.options.select_menu.all"));
    item.setValue(AjaxConstant.ALL);
    filter.addItem(item);
    
    Connection conn = null;
    try {
      conn = DataSourceFactory.getConnection();
      SysFunctionDAObject sysFunctionDAO = new SysFunctionDAObject(sessionContainer, conn);
      SysFunction tmpSysFunction = (SysFunction) sysFunctionDAO.getByFunctionCode(SystemFunctionConstant.SCH_CALENDAR);
      if ((GlobalConstant.STATUS_ACTIVE.equals(tmpSysFunction.getStatus()))
          && (!SystemFunctionConstant.MENU_TYPE_HIDE.equals(tmpSysFunction.getMenuType()))) {
        item = new SelectionFilterItem();
        item.setName(MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "home.label.icon_invite_meeting"));
        item.setValue(AjaxConstant.MEETINGINVITATION);
        filter.addItem(item);
        item = new SelectionFilterItem();
        item.setName(MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "home.label.icon_notification_meeting"));
        item.setValue(AjaxConstant.MEETINGNOIFICATION);
        filter.addItem(item);
      }
    } catch (Exception ex) {
      log.error(ex, ex);
    } finally {
      DbUtils.closeQuietly(conn);
    }
    
    item = new SelectionFilterItem();
    item.setName(MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "home.label.icon_invite_document"));
    item.setValue(AjaxConstant.DOCUMENTINVITATION);
    filter.addItem(item);
    item = new SelectionFilterItem();
    item.setName(MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "home.label.icon_notification_document"));
    item.setValue(AjaxConstant.DOCUMENTNOTIFICATION);
    filter.addItem(item);
    item = new SelectionFilterItem();
    item.setName(MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "home.label.icon_notification_workflow"));
    item.setValue(AjaxConstant.WORKFLOWNOTIFICATION);
    filter.addItem(item);

    return filter;
  }
  
  private static Filter getTaskInboxFilter(PageContext pageContext,String filterName){
    SessionContainer sessionCon = null;
    Connection conn = null;
    try{
    sessionCon = (SessionContainer)pageContext.getSession().getAttribute(GlobalConstant.SESSION_CONTAINER_KEY);
    conn = DataSourceFactory.getConnection();
    WorkflowProgressDAObject progressDAO = new WorkflowProgressDAObject(sessionCon,conn);
    UserDelegationDAObject delegationDAO = new UserDelegationDAObject(sessionCon,conn);
    List delegationList = delegationDAO.getListDelegatedToUser();
    progressDAO.setPendingUserListCount(delegationList);
   
    
    Filter filter = new SelectionFilter();
    filter.setName(filterName);
    FilterItem item = new SelectionFilterItem();
    item.setName(MessageResourcesFactory.getMessage(sessionCon.getSessionLocale(), "common.options.select_menu.all"));
    item.setValue("");
    item.setOption("");//all :should select
    filter.addItem(item);
    
    if(!Utility.isEmpty(delegationList)){
      item = new SelectionFilterItem();
      item.setName(MessageResourcesFactory.getMessage(sessionCon.getSessionLocale(), "workflow.label.my_task"));
      item.setValue(sessionCon.getUserRecordID().toString());
      filter.addItem(item);
    }
    
    for(Iterator it= delegationList.iterator();it.hasNext();){
      UserDelegation userDelegation = (UserDelegation)it.next();
      item = new SelectionFilterItem();
      item.setName(userDelegation.getSourceUserName());
      item.setValue(userDelegation.getSourceUserID().toString());
      filter.addItem(item);
    }
    
    return filter;
    }catch(Exception ex){
      log.error(ex, ex);
      return null;
    }finally{
      DbUtils.closeQuietly(conn);
    }
  }
  
}
